package com.baosight.risk.common.quartz;

import com.baosight.risk.api.fltd.FltdProcessService;
import com.baosight.risk.common.aspects.factory.AsyncFactory;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.constant.WorkflowConstant;
import com.baosight.risk.common.manager.AsyncManager;
import com.baosight.risk.common.utils.IDGenerator;
import com.baosight.risk.service.fltd.vo.FltdTaskAuthVO;
import com.baosight.risk.service.fltd.vo.FltdTaskVO;
import com.baosight.risk.service.system.entity.SysOperLog;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.genid.GenIdUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author duanyongqiang
 * <p>
 * 定时任务方法名
 */
@Slf4j
@Component("comTask")
public class CommTask {

    @Autowired
    private FltdProcessService fltdProcessService;

    public void testParams(String params, String jobId) throws Exception {
        List<SysOperLog> logs = new ArrayList<>();
        Exception error = null;
        try {
            log.error("进入测试定时任务");
//        testBatchRecordOper();

            System.out.println(params + "------" + jobId);

//        try {
////            test1();
//            test2();
//        } catch (Exception e){
//            log.error(e.getMessage());
//        }
            log.error("结束------测试定时任务");
            logs.add(fltdProcessService.getLog("测试定时任务执行成功", 0, "", jobId));
        }catch (Exception e) {
            error = e;
            logs.add(fltdProcessService.getLog("测试定时任务执行失败", 1, e.getMessage(), jobId));
        }
        // 保存日志到数据库
        AsyncManager.me().execute(AsyncFactory.batchRecordOper(logs));
        if (error != null) {
            throw error;
        }

    }

    public void testNoParams() throws Exception {
        log.error("执行无参方法");
        throw new Exception("错误");
    }

    private void testBatchRecordOper() {
        List<SysOperLog> operLogList = new ArrayList<>();
        //保存数据库

        SysOperLog log1 = (SysOperLog) BeanUtils.initBean(new SysOperLog());
        //SysOperLog log1 = new SysOperLog();
        log1.setId("00000");
        log1.setBusinessType(4);
        log1.setMethod("调用方法111");
        log1.setErrorMsg("异常信息");
        log1.setStatus(1);
        operLogList.add(log1);


        SysOperLog log2 = new SysOperLog();
        log2.setId("00001");
        log2.setBusinessType(4);
        log2.setMethod("调用方法2222");
        log2.setErrorMsg("正常信息");
        log2.setStatus(0);
        operLogList.add(log2);

        // 保存数据库
        AsyncManager.me().execute(AsyncFactory.batchRecordOper(operLogList));
    }

    private void test1() throws Exception {
//        XtzyUser user = new XtzyUser();
//        user.setId("2da02f7a-b74a-433f-97e8-2df36615178b");
//        user.setUserCode("cuishihao");
//        user.setUserDesc("Chris Cui");
//        user.setExt1("BGTAZA00");
//        user.setExt2("股份-办公室");
        FltdTaskVO vo = new FltdTaskVO();
        vo.setFlowNo("MYDEMO001");
        vo.setFlowName("测试流程");
//            vo.setFlowNo("fengxiandangan");
        vo.setAcctYear("2019");
        vo.setDeptCode("BGTAZA00");
        vo.setDeptName("股份-办公室");
        vo.setBizGuid(UUID.randomUUID().toString());
        vo.setBizName("流程测试");
        fltdProcessService.doStart(vo);
    }

    private void test2() throws Exception {
        FltdTaskVO taskVO = new FltdTaskVO();
        taskVO.setFlowNo("MYDEMO006");
        taskVO.setFlowName("测试流程");
        taskVO.setAcctYear("2019");
        taskVO.setBizName("测试");
        taskVO.setBizDesc("测试");
        taskVO.setQuarterCode("3");
        taskVO.setQuarterName("三季度");

        XtzyUser xtzyUser = new XtzyUser();
        xtzyUser.setExt1("BGTAZMC0");
        xtzyUser.setExt2("法律事务室");
        xtzyUser.setUserCode("lishaoqing");
        xtzyUser.setUserDesc("李少清");
        taskVO.setBizGuid(UUID.randomUUID().toString());
        List<XtzyUser> userList = new ArrayList<>();
        userList.add(xtzyUser);
        taskVO.setCustomUsers(userList);
        fltdProcessService.doStart(taskVO);

        xtzyUser = new XtzyUser();
        xtzyUser.setExt1("BGTAZAA0");
        xtzyUser.setExt2("文秘室");
        xtzyUser.setUserCode("test");
        xtzyUser.setUserDesc("test员工");
        taskVO.setBizGuid(UUID.randomUUID().toString());
        userList = new ArrayList<>();
        userList.add(xtzyUser);
        taskVO.setCustomUsers(userList);

        fltdProcessService.doStart(taskVO);


    }


}
